<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Peixunyanlian;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\Dept;
use App\Admin\Renderable\DeptTable;


class PeixunyanlianController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Peixunyanlian(), function (Grid $grid) {
            // $grid->column('id')->sortable();
            $grid->model()->orderBy('peixundate', 'desc');

            $grid->column('peixundate');
            $grid->model()->with(['dept']);
            $grid->column('dept.deptname', '部门')->link(function ($value) {
                $deptid = Dept::where('deptname', $value)->first()->id;
                return admin_url('depts/' . $deptid);
            });
            $grid->column('didian');
            $grid->column('leixing');

            $grid->leixing->using([0 => '培训+演练', 1 => '培训', 2 => '演练']);

            $grid->column('peixunneirong');

            $grid->column('peixunpics')->display(function ($imgs) {
                $pics = json_decode($imgs);
                if ($pics) {
                    return $pics[0];
                }
            })->image();

            // $grid->column('created_at');
            // $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $repository = new Peixunyanlian(['dept']);

        return Show::make($id, $repository, function (Show $show) {
            $show->field('id');
            $show->field('peixundate');
            $show->field('dept.deptname', '部门');
            $show->field('didian');
            $show->leixing->using([0 => '培训+演练', 1 => '培训', 2 => '演练']);
            $show->field('peixunneirong');
            $show->peixunpics()->image();
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Peixunyanlian(), function (Form $form) {
            $form->display('id');
            $form->date('peixundate')->required();
            // $form->text('dept_id');
            $form->selectTable('dept_id', '部门：')
                ->title('部门列表')
                ->from(DeptTable::make())
                ->model(Dept::class, 'id', 'deptname')->required()->width(3);

            $form->text('didian')->required();
            // $form->text('leixing');
            $form->radio('leixing')->options(['培训+演练', '培训', '演练'])->default(0)->required();
            $form->textarea('peixunneirong')->required();

            $form->multipleImage('peixunpics')->accept('jpg,png,gif,jpeg')->limit(9)->autoUpload()->maxSize(20480)->saving(function ($files) {
                return json_encode($files);
            });
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
